package cn.tedu.cwgl.controller;

import cn.tedu.cwgl.entity.Card;
import cn.tedu.cwgl.entity.UserInfo;
import cn.tedu.cwgl.service.CardService;
import cn.tedu.cwgl.util.Config;
import cn.tedu.cwgl.util.PageModel;
import cn.tedu.cwgl.util.Result;
import cn.tedu.cwgl.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/card")
public class CardController {

    @Autowired
    CardService cardService;

    @RequestMapping("/getCards/{pageNo}/{pageSize}/{type}")
    @ResponseBody
    public Result<Card> getCards(@PathVariable int pageNo, @PathVariable int pageSize,
                                 @PathVariable String type, Card card, HttpSession session) {

        if (!type.equals("-1")) {
            for (Card card1 : cardService.getCardType()) {
                if (card1.getId().toString().equals(type)) {
                    card.setTypename(card1.getTypename());
                    break;
                }
            }
        }
        UserInfo userInfo = (UserInfo) session.getAttribute(Config.CURRENT_USERNAME);
        if (userInfo.getRoleid() == 2) {
            card.setHouseid(userInfo.getHouseid());
        }
        if (userInfo.getRoleid() == 3) {
            card.setId(userInfo.getId());
        }
        PageModel model = new PageModel<>(pageNo, card);
        model.setPageSize(pageSize);
        Result<Card> result = cardService.getCard(model);
        return result;
    }

    @RequestMapping("getCardType")
    @ResponseBody
    public Result<Card> getCardType() {

        List<Card> cardType = cardService.getCardType();
        return ResultUtil.success(cardType);
    }

    @RequestMapping("/updateCard")
    @ResponseBody
    public Result updateCard(Card card) {
        int updateCard = cardService.updateCard(card);
        if (updateCard > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.unSuccess();
    }

    @RequestMapping("/delCard")
    @ResponseBody
    public Result delCard(int id) {
        int result = cardService.delCard(id);
        if (result > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.unSuccess();
    }

    @RequestMapping("/addCard")
    @ResponseBody
    public Result addCard(Card card, HttpSession session) {
        UserInfo userInfo = (UserInfo) session.getAttribute(Config.CURRENT_USERNAME);
        card.setUserid(userInfo.getId());
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String format = dateFormat.format(calendar.getTime());
        card.setCreatetime(format);
        int result = cardService.addCard(card);
        if (result > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.unSuccess();
    }

}
